import 'package:flutter/material.dart'; // 引入 Flutter 材料库
import 'dart:core'; // 引入 StringBuffer

void main() {
  var paint =
      Paint()
        ..color = Colors.black
        ..strokeCap = StrokeCap.round
        ..strokeWidth = 5.0;
  print(
    'paint: $paint, color: ${paint.color}, strokeCap: ${paint.strokeCap}, strokeWidth: ${paint.strokeWidth}',
  );

  // 等价于
  var paint2 = Paint();
  paint2.color = Colors.black;
  paint2.strokeCap = StrokeCap.round;
  paint2.strokeWidth = 5.0;
  print(
    'paint2: $paint2, color: ${paint2.color}, strokeCap: ${paint2.strokeCap}, strokeWidth: ${paint2.strokeWidth}',
  );

  // 由于querySelector在dart:html中，这里使用一个替代方案
  // 演示级联操作后移除了未使用的button变量
  Button()
    ..textContent = 'Confirm'
    ..classList.add('important')
    ..onClick = (e) {
      print('Confirmed!');
    }
    ..scrollIntoView();

  // 修正嵌套级联操作的括号和缩进
  final phoneBuilder =
      PhoneNumberBuilder()
        ..number = '415-555-0100'
        ..label = 'home';

  final phoneNumber = phoneBuilder.build();

  final addressBookBuilder =
      AddressBookBuilder()
        ..name = 'jenny'
        ..email = 'jenny@example.com'
        ..phone = phoneNumber;

  final addressBook = addressBookBuilder.build();

  print(
    'addressBook name: ${addressBook.name}, email: ${addressBook.email}, phone number: ${addressBook.phone.number}, label: ${addressBook.phone.label}',
  );

  var sb = StringBuffer();
  sb.write('foo');
  sb.write('bar'); // 不会报错，write方法返回void，级联可以继续执行, 只是后面的write没有效果。
  print('string buffer : $sb');
}

// querySelector('#confirm')的替代
class Button {
  String? textContent;
  final List<String> classList = [];
  void Function(dynamic)? onClick;
  void scrollIntoView() {
    print('scrolling into view');
  }
}

class AddressBookBuilder {
  String? name;
  String? email;
  PhoneNumber? phone; // 修改为 PhoneNumber? 类型

  AddressBook build() {
    return AddressBook(name: name!, email: email!, phone: phone!);
  }
}

class AddressBook {
  final String name;
  final String email;
  final PhoneNumber phone;

  AddressBook({required this.name, required this.email, required this.phone});
}

class PhoneNumberBuilder {
  String? number;
  String? label;

  PhoneNumber build() {
    return PhoneNumber(number: number!, label: label!);
  }
}

class PhoneNumber {
  final String number;
  final String label;

  PhoneNumber({required this.number, required this.label});
}
